// pages/edit/edit.js
const {detail, option, update, add, list, follow, faceMatch, session, rubbish, levelOption, baiduIdentify} = require("../../api/index.js")
const des = require('../../utils/des.js')
const utils = require("../../utils/index.js")
Page({

/**
* 页面的初始数据
*/data: {
imgIcon: "../../static/upload.png",
editStatus: false,
baseURL:'',
sessionReadArr:[],
detailList: null,
id: "",
cross:"",
ruleForm:{},
userid:getApp().globalData.userInfo.id,
userInfo:getApp().globalData.userInfo,
ro:{
},
xiangmumingcheng:"",
xiangmufenleiIndex: 0,
xiangmufenleiList: [],
xiangmujieduanList:"萌创期,初创期,孵创期".split(','),
xiangmujieduanIndex:0,
xiangmudangqianjieduan:"",
suoshulingyu:"",
suoshuxingye:"",
xiangmumubiao:"",
suoxuziyuan:"",
xiangmujianjie:"",
jinzhanjihua:"",
xiangmutupian:"",
yuqichengguo:"",
xiangmuxiangqing:"",
tijiaoshijian:"请选择时间",
showtijiaoshijian:false,
sfsh: '待审核',
shhf:"",
fuzerenzhanghao:"",
fuzerenxingming:"",
chengshi:"",
xianqu:"",
jiedao:"",
xuexiaomingcheng:"",
xueyuanmingcheng:"",
userid:"",
xiangmujianjie:"",
jinzhanjihua:"",
},
/**
* 生命周期函数--监听页面加载
*/
async onLoad(options) {
let userid
if (options?.id) {
this.setData({
editStatus: true
})
}
let nowTable = wx.getStorageSync("nowTable");
const res = await session(nowTable)
if(res.data.code==0){
getApp().globalData.userInfo=res?.data
userid = res?.data.id
this.setData({
userid
})
}
let baseURL =wx.getStorageSync('baseURL') + '/'
const id = getApp().globalData.detailId
this.setData({
refid:id,
baseURL
})
//人脸识别
const xiangmufenleires = await option('xiangmufenlei/xiangmufenlei')
xiangmufenleires.data.unshift('请选择项目分类')
this.setData({
xiangmufenleiList: xiangmufenleires.data
})
 let xiangmujieduanList="萌创期,初创期,孵创期".split(',');
 let xiangmujieduanIndex=0;
 this.setData({
   xiangmujieduanList,
   xiangmujieduanIndex
 })
//下 == "下" ${column.refTable} && ${column.refColumn}
 this.setData({
   sfsh: '待审核',
 })
let  ro=this.data.ro;
if(options?.cross){
var obj = wx.getStorageSync('crossObj');
let refobjempty={}
for (var o in obj){
if(o=='xiangmumingcheng'){
   refobjempty["xiangmumingcheng"]=obj[o]
ro.xiangmumingcheng = true;
continue;
}else{
}
if(o=='xiangmufenlei'){
   refobjempty["xiangmufenlei"]=obj[o]
ro.xiangmufenlei = true;
continue;
}else{
}
if(o=='xiangmujieduan'){
   refobjempty["xiangmujieduan"]=obj[o]
ro.xiangmujieduan = true;
continue;
}else{
}
if(o=='xiangmudangqianjieduan'){
   refobjempty["xiangmudangqianjieduan"]=obj[o]
ro.xiangmudangqianjieduan = true;
continue;
}else{
}
if(o=='suoshulingyu'){
   refobjempty["suoshulingyu"]=obj[o]
ro.suoshulingyu = true;
continue;
}else{
}
if(o=='suoshuxingye'){
   refobjempty["suoshuxingye"]=obj[o]
ro.suoshuxingye = true;
continue;
}else{
}
if(o=='xiangmumubiao'){
   refobjempty["xiangmumubiao"]=obj[o]
ro.xiangmumubiao = true;
continue;
}else{
}
if(o=='suoxuziyuan'){
   refobjempty["suoxuziyuan"]=obj[o]
ro.suoxuziyuan = true;
continue;
}else{
}
if(o=='xiangmujianjie'){
   refobjempty["xiangmujianjie"]=obj[o]
ro.xiangmujianjie = true;
continue;
}else{
}
if(o=='jinzhanjihua'){
   refobjempty["jinzhanjihua"]=obj[o]
ro.jinzhanjihua = true;
continue;
}else{
}
if(o=='xiangmutupian'){
refobjempty['xiangmutupian']=obj[o];
refobjempty['xiangmutupianPath']=obj[o].split(",")[0];
ro.xiangmutupian = true;
continue;
}else{
}
if(o=='yuqichengguo'){
   refobjempty["yuqichengguo"]=obj[o]
ro.yuqichengguo = true;
continue;
}else{
}
if(o=='xiangmuxiangqing'){
getApp().globalData.editorContent=obj[o]
ro.xiangmuxiangqing = true;
continue;
}else{
}
if(o=='tijiaoshijian'){
   refobjempty["tijiaoshijian"]=obj[o]
ro.tijiaoshijian = true;
continue;
}else{
}
if(o=='fuzerenzhanghao'){
   refobjempty["fuzerenzhanghao"]=obj[o]
ro.fuzerenzhanghao = true;
continue;
}else{
}
if(o=='fuzerenxingming'){
   refobjempty["fuzerenxingming"]=obj[o]
ro.fuzerenxingming = true;
continue;
}else{
}
if(o=='chengshi'){
   refobjempty["chengshi"]=obj[o]
ro.chengshi = true;
continue;
}else{
}
if(o=='xianqu'){
   refobjempty["xianqu"]=obj[o]
ro.xianqu = true;
continue;
}else{
}
if(o=='jiedao'){
   refobjempty["jiedao"]=obj[o]
ro.jiedao = true;
continue;
}else{
}
if(o=='xuexiaomingcheng'){
   refobjempty["xuexiaomingcheng"]=obj[o]
ro.xuexiaomingcheng = true;
continue;
}else{
}
if(o=='xueyuanmingcheng'){
   refobjempty["xueyuanmingcheng"]=obj[o]
ro.xueyuanmingcheng = true;
continue;
}else{
}
if(o=='userid'){
   refobjempty["userid"]=obj[o]
ro.userid = true;
continue;
}else{
}
}
let  statusColumnName=wx.getStorageSync('statusColumnName');
statusColumnName=statusColumnName.replace('[',"").replace(']',"");
this.setData({
ro,
cross:options?.cross,
statusColumnName
})
this.setData(refobjempty)
}
if(id){
// 如果上一级页面传递了id，获取改id数据信息
const data=getApp().globalData.detailList;
let dataEmpty={};
xiangmufenleires.data.map((item, index) => {
if (item == data.xiangmufenlei) {
 this.setData({
xiangmufenleiIndex: index,
xiangmufenlei: item
 })
}else if (this.data.xiangmufenleiList.length == 1) {
this.setData({
xiangmufenleiIndex: 0,
xiangmufenlei: v
 })
}
})
const  def_3= 0  ;
this.data.xiangmujieduanList.map((v, index) => {
if (v == data.xiangmujieduan|| (v == def_3 && def_3 !=null)) {
 this.setData({
   xiangmujieduanIndex: index,
   xiangmujieduan: v,
 })
}else if (this.data.xiangmujieduanList.length == 1) {
 this.setData({
   xiangmujieduanIndex: 0,
   xiangmujieduan: v,
 })
}
})
for (var o in data){
if(o=='xiangmumingcheng') {
 if(!this.data.xiangmumingcheng){
  dataEmpty["xiangmumingcheng"] = data[o]
 }
}
if(o=='xiangmufenlei') {
 if(!this.data.xiangmufenlei){
  dataEmpty["xiangmufenlei"] = data[o]
 }
}
if(o=='xiangmujieduan') {
 if(!this.data.xiangmujieduan){
  dataEmpty["xiangmujieduan"] = data[o]
 }
}
if(o=='xiangmudangqianjieduan') {
 if(!this.data.xiangmudangqianjieduan){
  dataEmpty["xiangmudangqianjieduan"] = data[o]
 }
}
if(o=='suoshulingyu') {
 if(!this.data.suoshulingyu){
  dataEmpty["suoshulingyu"] = data[o]
 }
}
if(o=='suoshuxingye') {
 if(!this.data.suoshuxingye){
  dataEmpty["suoshuxingye"] = data[o]
 }
}
if(o=='xiangmumubiao') {
 if(!this.data.xiangmumubiao){
  dataEmpty["xiangmumubiao"] = data[o]
 }
}
if(o=='suoxuziyuan') {
 if(!this.data.suoxuziyuan){
  dataEmpty["suoxuziyuan"] = data[o]
 }
}
if(o=='xiangmujianjie') {
 if(!this.data.xiangmujianjie){
  dataEmpty["xiangmujianjie"] = data[o]
 }
}
if(o=='jinzhanjihua') {
 if(!this.data.jinzhanjihua){
  dataEmpty["jinzhanjihua"] = data[o]
 }
}
if(o=='xiangmutupian') {
dataEmpty['xiangmutupian'] = data[o];
dataEmpty['xiangmutupianPath'] = data[o].split(",")[0];
}
if(o=='yuqichengguo') {
 if(!this.data.yuqichengguo){
  dataEmpty["yuqichengguo"] = data[o]
 }
}
if(o=='xiangmuxiangqing') {
getApp().globalData.editorContent = data[o];
}
if(o=='tijiaoshijian') {
 if(!this.data.tijiaoshijian){
  dataEmpty["tijiaoshijian"] = data[o]
 }
}
if(o=='fuzerenzhanghao') {
 if(!this.data.fuzerenzhanghao){
  dataEmpty["fuzerenzhanghao"] = data[o]
 }
}
if(o=='fuzerenxingming') {
 if(!this.data.fuzerenxingming){
  dataEmpty["fuzerenxingming"] = data[o]
 }
}
if(o=='chengshi') {
 if(!this.data.chengshi){
  dataEmpty["chengshi"] = data[o]
 }
}
if(o=='xianqu') {
 if(!this.data.xianqu){
  dataEmpty["xianqu"] = data[o]
 }
}
if(o=='jiedao') {
 if(!this.data.jiedao){
  dataEmpty["jiedao"] = data[o]
 }
}
if(o=='xuexiaomingcheng') {
 if(!this.data.xuexiaomingcheng){
  dataEmpty["xuexiaomingcheng"] = data[o]
 }
}
if(o=='xueyuanmingcheng') {
 if(!this.data.xueyuanmingcheng){
  dataEmpty["xueyuanmingcheng"] = data[o]
 }
}
if(o=='userid') {
 if(!this.data.userid){
  dataEmpty["userid"] = data[o]
 }
}
}
const url = wx.getStorageSync("baseURL") + "/"
const detailList = data
let  objtemp= {
detailList,
 xiangmutupian:data?.xiangmutupian?.split(',')[0],
 xiangmutupianPath:data?.xiangmutupian?.split(',')[0],
 tijiaoshijian:utils.getCurrentDate("yMDhms"),
 sfsh: '待审核',
}
this.data.editStatus? getApp().globalData.editorContent=data.xiangmuxiangqing:""
this.setData(objtemp);

this.setData(dataEmpty)
}else {
this.setData({
})
}
// ss读取
let sessionReadArr=[]
 if(!this.data.fuzerenzhanghao&&getApp().globalData.userInfo.fuzerenzhanghao){
 let fuzerenzhanghao= getApp().globalData.userInfo.fuzerenzhanghao
 ro.fuzerenzhanghao=true
 this.setData({
   fuzerenzhanghao,
  })
 sessionReadArr.push('fuzerenzhanghao')
 }
 if(!this.data.fuzerenxingming&&getApp().globalData.userInfo.fuzerenxingming){
 let fuzerenxingming= getApp().globalData.userInfo.fuzerenxingming
 ro.fuzerenxingming=true
 this.setData({
   fuzerenxingming,
  })
 sessionReadArr.push('fuzerenxingming')
 }
this.setData({
cross:options?.cross,
ro,
id,
})

if (wx.getStorageSync('raffleType') ) {
 wx.removeStorageSync('raffleType')
 setTimeout(() => {
  this.submit()
 }, 300)
}














this.setData({
tijiaoshijian:utils.getCurrentDate("yMDhms")
})











},
getUUID () {
return new Date().getTime();
},
onUnload: function () {
getApp().globalData.editorContent = ''
getApp().globalData.editorContent = ''
getApp().globalData.editorContent = ''
},
onShow() {
},
 commentScore(e){
 if(this.data?.commentScore){
  this.setData({
   commentScore:e.detail
  })
 }
 },






//xiangmufenlei, 0
// 下拉变化
xiangmufenleiChange(e) {
this.setData({
 xiangmufenleiIndex:   e.detail.value,
 xiangmufenlei :this.data.xiangmufenleiList[e.detail.value]
})
},

//xiangmujieduan, 0
// 下拉变化
xiangmujieduanChange(e) {
this.setData({
 xiangmujieduanIndex:   e.detail.value,
 xiangmujieduan :this.data.xiangmujieduanList[e.detail.value]
})
},


































xiangmujianjieInput(e){
this.setData({
xiangmujianjie: e.detail.value // 每次输入变化时更新文本框的值
});},




xiangmujianjieInput(e) {
this.setData({
xiangmujianjie: e.detail.value // 每次输入变化时更新文本框的值
});
},
jinzhanjihuaInput(e){
this.setData({
jinzhanjihua: e.detail.value // 每次输入变化时更新文本框的值
});},




jinzhanjihuaInput(e) {
this.setData({
jinzhanjihua: e.detail.value // 每次输入变化时更新文本框的值
});
},
uploadxiangmutupian() {
wx.chooseImage({
count: 1,
sizeType: ['compressed'],
sourceType: ['album', 'camera'],
success: async (res) => {
const tempFilePaths = res.tempFilePaths;
// 本地临时图片的路径
this.setData({
 xiangmutupianPath: tempFilePaths[0]
})
let _this = this;
// 上传网络图片
const baseURL = wx.getStorageSync('baseURL')
wx.uploadFile({
url: `${baseURL}/file/upload`,
filePath: res.tempFilePaths[0],
name: 'file',
header: {
 'Token': wx.getStorageSync('token')
},
success: (uploadFileRes) => {
 let result = JSON.parse(uploadFileRes.data);
// result.file是上传成功为网络图片的名称
 if (result.code == 0) {
  this.setData({
    xiangmutupian: 'file/' + result.file
  })
 } else {
  wx.showToast({
   title: result.msg,
   icon: 'none',
   duration: 2000
  });
 }
}
})

this.setData({
face: tempFilePaths[0]
});

}
})
},



















 ontijiaoshijianTap(){
this.setData({
showtijiaoshijian: true,
})
let c = this.data;
},
tijiaoshijianTap(e) {
this.setData({
tijiaoshijian: e.detail.data
})

let c = this.data;
},




shhfInput(e){
this.setData({
shhf: e.detail.value // 每次输入变化时更新文本框的值
});},




shhfInput(e) {
this.setData({
shhf: e.detail.value // 每次输入变化时更新文本框的值
});
},


































async submit() {
let that = this
var query = wx.createSelectorQuery();

// 在 Page 中的某个方法中调用
query.select('#xiangmujianjie').boundingClientRect();
query.exec(function (res) {
//res就是 所有标签为v1的元素的信息 的数组
that.setData({
xiangmujianjie: res[0]?.dataset?.info
})
})
// 在 Page 中的某个方法中调用
query.select('#jinzhanjihua').boundingClientRect();
query.exec(function (res) {
//res就是 所有标签为v1的元素的信息 的数组
that.setData({
jinzhanjihua: res[0]?.dataset?.info
})
})
if(this.data.tijiaoshijian?.includes("请选择") || this.data.tijiaoshijian==""){
wx.showToast({
icon: "none",
title: `提交时间不能为空`,
})
return
}

const baseURL = wx.getStorageSync('baseURL') + "/"
const regex = new RegExp(baseURL, "g");
const obj={
xiangmumingcheng: this.data.xiangmumingcheng,
xiangmufenlei:this.data.xiangmufenleiList[this.data.xiangmufenleiIndex]?this.data.xiangmufenleiList[this.data.xiangmufenleiIndex]:'',
xiangmujieduan:this.data.xiangmujieduanList[this.data.xiangmujieduanIndex]?this.data.xiangmujieduanList[this.data.xiangmujieduanIndex]:'',
xiangmudangqianjieduan: this.data.xiangmudangqianjieduan,
suoshulingyu: this.data.suoshulingyu,
suoshuxingye: this.data.suoshuxingye,
xiangmumubiao: this.data.xiangmumubiao,
suoxuziyuan: this.data.suoxuziyuan,
xiangmujianjie: this.data.xiangmujianjie,
jinzhanjihua: this.data.jinzhanjihua,
xiangmutupian:this.data.xiangmutupian?.replace(regex, ""),
yuqichengguo: this.data.yuqichengguo,
xiangmuxiangqing: getApp().globalData.editorContent,
tijiaoshijian: this.data.tijiaoshijian,
shhf:this.data.shhf,
fuzerenzhanghao: this.data.fuzerenzhanghao,
fuzerenxingming: this.data.fuzerenxingming,
chengshi: this.data.chengshi,
xianqu: this.data.xianqu,
jiedao: this.data.jiedao,
xuexiaomingcheng: this.data.xuexiaomingcheng,
xueyuanmingcheng: this.data.xueyuanmingcheng,
userid: this.data.userid,
}
const detailId= getApp().globalData.detailId
const tableName= `xiangmuxinxi`
//跨表计算判断
var obj2;
var  ruleForm=obj
obj2 = ruleForm
this.data.refid==""? ruleForm['refid']= getApp().globalData.detailId:""
ruleForm['userid']=getApp().globalData.userInfo.id;
var userInfo=getApp().globalData.userInfo
const sessionReadArr=this.data.sessionReadArr
const phonePattern = /^\(?\d{3}\)?[-.\s]?\d{3}[-.\s]?\d{4}$/;
const mobilePattern = /^(?:\+?86)?1[3-9]\d{9}$/;
const emailPattern = /^[A-Za-z0-9._%+-]+@[A-Za-z0-9.-]+\.[A-Za-z]{2,}$/;
const idPattern = /^[1-9]\d{5}(18|19|20)\d{2}(0[1-9]|1[0-2])(0[1-9]|[1-2]\d|3[0-1])\d{3}[\dxX]$/;
const urlPattern = /^(http|https):\/\/[\w\-]+(\.[\w\-]+)+[/#?]?.*$/;
 if(!this.data.xiangmumingcheng ){
  wx.showToast({
   icon: "none",
   title: `项目名称不能为空`,
  })
  return
 }





 if(!this.data.xiangmufenlei ){
  wx.showToast({
   icon: "none",
   title: `项目分类不能为空`,
  })
  return
 }





























 if(!this.data.xiangmujianjie ){
  wx.showToast({
   icon: "none",
   title: `项目简介不能为空`,
  })
  return
 }





















let xiangmuxiangqing=  getApp().globalData.editorContent
ruleForm['xiangmuxiangqing']=getApp().globalData.editorContent
if(xiangmuxiangqing==='' ){
wx.showToast({
icon: "none",
title: `项目详情不能为空`,
})
return
}













































//更新跨表属性
var crossuserid;
var crossrefid;
var crossoptnum;
if(this.data.cross) {
wx.setStorageSync('crossCleanType', true);
var statusColumnName = wx.getStorageSync('statusColumnName');
var statusColumnValue = wx.getStorageSync('statusColumnValue');
if (statusColumnName != '') {
obj2 = wx.getStorageSync('crossObj');
if (!statusColumnName.startsWith("[")) {
for (var o in obj2) {
if (statusColumnName==o){
obj2[o] = statusColumnValue;
}

}
var table = wx.getStorageSync('crossTable');

await update(table, obj2)
} else {

crossuserid =getApp().globalData.userInfo.id
crossrefid =  this.data.id
crossoptnum = wx.getStorageSync('statusColumnName');
crossoptnum = crossoptnum.replace(/\[/, "").replace(/\]/, "");
}
}
}
 ruleForm?.shhf?ruleForm.shhf=this.data.shhf:''
if(crossrefid && crossuserid) {
ruleForm['crossuserid'] =getApp().globalData.userInfo.id;
ruleForm['crossrefid'] =obj2.id;
this.setData({
ruleForm
})
let params = {
page: 1,
limit: 10,
crossuserid: crossuserid,
crossrefid: crossrefid,
}
const tips = wx.getStorageSync('tips')
let corssRes = await list(`xiangmuxinxi`, params)
crossoptnum = wx.getStorageSync('statusColumnName');
crossoptnum = crossoptnum.match(/\d+/g);
if (corssRes?.data?.total >= parseInt(crossoptnum)) {
wx.showToast({
icon: "none",
title: tips,
})
wx.removeStorageSync('crossCleanType');
return ;
}
else {
//跨表计算
if (ruleForm.id ) {
await update(`xiangmuxinxi`, ruleForm)
}
else {
 this.data?.commentScore?ruleForm['score']= this.data.commentScore:'';
 await add(`xiangmuxinxi`, ruleForm)
}
}
}
else {
//跨表计算
if (ruleForm.id || this.data.editStatus) {
this.data.editStatus?ruleForm['id']= getApp().globalData.detailId:""
await update(`xiangmuxinxi`, ruleForm)
}
else {
 this.data?.commentScore?ruleForm['score']= this.data.commentScore:'';
await add(`xiangmuxinxi`, ruleForm)
}
}
getApp().globalData.editorContent=''
wx.showToast({
title: '提交成功',
icon: "none"
})
const preId = getApp().globalData.detailId

if(table){
let res = await detail(table, preId)
if(res.code==0){
getApp().globalData.detailList = res.data
}

}
wx.navigateBack({
delta: 1,
complete: () => {
// 触发事件通知，传递需要更新的数据
const pages = getCurrentPages();
if (pages.length >= 1) {
const prePage = pages[pages.length - 1];
prePage.onLoad(); //
}
}
})

},
onHide() {

},


/**
* 生命周期函数--监听页面卸载
*/
onUnload() {

},


/**
* 页面相关事件处理函数--监听用户下拉动作
*/
onPullDownRefresh() {

},

/**
* 页面上拉触底事件的处理函数
*/
onReachBottom() {

},

/**
* 用户点击右上角分享
*/
onShareAppMessage() {

}
})